/**
 * gome
 * created by gaoqw on 2015-11-24
 */
var root = window || {};
root.util = root.util || {};
var GameIframe = function(a) {
    this.options = $.extend({goods: [], detailPageURL: "detail.html", fvDefVal: "100元", selector: ""}, a), this.sel = this.options.selector, this.el = $(this.sel)
};
$.extend(GameIframe.prototype, {init: function() {
        var a = this;
        a.eventCode(), a.initInput(), a.load(), a.getAd()
    }, load: function() {
        this.getGame()
    }, initInput: function(a) {
        var b = this, c = a || $("input[diyplaceholder]:not([disabled])", b.el);
        c.each(function(a, b) {
            $(b).val($(b).attr("diyplaceholder")).css("color", "#999"), $(b).off("click").on("click", function() {
                $(b).is("[isSelect]") || $(b).attr("diyplaceholder") != $(b).val() || $(b).val("").css("color", "#666")
            }), $(b).off("blur").on("blur", function() {
                ("" == $(b).val() || $(b).attr("diyplaceholder") == $(b).val()) && $(b).val($(b).attr("diyplaceholder")).css("color", "#999")
            })
        })
    }, clearSelect: function(a) {
        var b = this, c = "";
        c = a.join(", "), $(c, b.el).each(function(a, b) {
            $(b).val($(b).attr("diyplaceholder")).css("color", "#999").removeAttr("val"), $(b).closest("div").find(".data-list").html("")
        }), $("#price", b.el).html("¥0.00").hide()
    }, updateUlPosition: function(a, b) {
        var b = b || $(window.parent.document).find("iframe").height(), c = a.find(".data-list"), d = 0, e = 0, f = a.find(".value"), g = (f.offset().top, 0), h = !1;
        c.css({height: "auto", "margin-top": "0", "overflow-y": "hidden"}), d = c.offset().top, e = d + c.height(), e > b && (g = -1 * (e - b) - (b - c.height()) / 2, g += 15), c.height() > b && (g = -1 * d, h = !0, g += 15), h && c.css({height: b - 10 + "px", "overflow-y": "scroll"}), c.css({"margin-top": g + "px"})
    }, getAd: function() {
        var a = $("[adid]");
        a.each(function() {
            var a = $(this), b = Number(a.attr("adid")), c = !1;
            return b && $.ajax({url: "/v1/ad/getADs", type: "get", data: {boxid: b}, success: function(b) {
                    var c = b.data || {};
                    $.each(c.grps, function(b, c) {
                        $.each(c.ads, function(b, d) {
                            var e = d.furl, f = d.title, g = d.rurl;
                            switch (c.type) {
                                case 2:
                                    a.attr({href: e}).find("img").attr("src", g);
                                    break;
                                default:
                                    a.attr({href: e}).html(f)
                                }
                        })
                    })
                }, error: function(a, b) {
                    c = !0
                }}), c ? !1 : void 0
        })
    }, getGoodsById: function(a) {
        var b = this, c = {};
        return $.each(b.options.goods, function(b, d) {
            return a == d.cateid ? (c = d, !1) : void 0
        }), c
    }, getItem: function(a, b, c) {
        var d = c ? a[b + 1] : a[b - 1], b = c ? b + 1 : b - 1;
        return d && 2 == d.length ? void getItem(a, b, c) : d || {}
    }, getGame: function() {
        var a = this;
        util.api({url: "gamelist", type: "get", beforeSend: function() {
            }, success: function(b) {
                var c, d, e, f = b.body || [], g = "";
                200 == b.rpco && ($.each(f, function(b, h) {
                    c = h.idxfc, 2 == f[b].idxfc.length && (d = f[b].idxfc.slice(0, 1), e = f[b].idxfc.slice(1, 2), f[b - 1] && f[b + 1] ? c = a.getItem(f, b - 1, !1).idxfc <= d && d <= a.getItem(f, b + 1, !0).idxfc ? d : e : f[b - 1] ? c = a.getItem(f, b - 1, !1).idxfc <= d ? d : e : f[b + 1] && (c = d <= a.getItem(f, b + 1, !0).idxfc ? d : e)), g += '<li val="' + h.gdid + '" title="' + h.bdzn + '">' + c + "-" + h.bdzn + "</li>"
                }), $("#allGame .data-list", a.el).html(g))
            }})
    }, getGpitByGdid: function() {
        var a = this;
        util.api({url: "gamedetail", data: {gdid: $("#allGame .value", a.el).attr("val")}, async: !1, type: "get", beforeSend: function() {
            }, success: function(b) {
                var c = b.body || {}, d = c.gpit || [], e = "";
                200 == b.rpco && ($.each(d, function(a, b) {
                    e += '<li val="' + b.platid + '" title="' + b.gpzn + '">' + b.gpzn + "</li>"
                }), $("#gpit .data-list", a.el).html(e))
            }})
    }, getGrtitByPlatid: function() {
        var a = this;
        util.api({url: "platforminfo", data: {gdid: $("#allGame .value", a.el).attr("val"), platid: $("#gpit .value", a.el).attr("val")}, type: "get", beforeSend: function() {
            }, success: function(b) {
                var c = b.body || {}, d = c.grtit || [];
                200 == b.rpco && ($("#grtit a, #grtit label", a.el).hide(), $.each(d, function(b, c) {
                    $('#grtit a[val="' + c + '"]', a.el).show().next().show()
                }), $("#grtit a").removeClass("checked"), $("#grtit a:visible:first", a.el).length && $("#grtit a:visible:first", a.el).click())
            }})
    }, getFaceValueByGrtit: function() {
        var a = this;
        util.api({url: "goodslist", data: {gdid: $("#allGame .value", a.el).attr("val"), platid: $("#gpit .value", a.el).attr("val"), grtp: $("#grtit .checked", a.el).attr("val")}, type: "get", beforeSend: function() {
            }, success: function(b) {
                var c, d = b.body || [], e = "";
                200 == b.rpco && (a.options.goods = d, $.each(d, function(b, d) {
                    e += '<li val="' + d.cateid + '">' + d.gdfn + "</li>", d.gdfn === a.options.fvDefVal && (c = d.cateid)
                }), $("#goods .data-list", a.el).html(e), c ? $("#goods li[val=" + c + "]").click() : $("#goods li:first").click())
            }})
    }, getPrice: function() {
        var a = this, b = a.getGoodsById($("#goods .value", a.el).attr("val")), c = (b.sp / 100).toFixed(2);
        isNaN(c) && (c = "0.00"), $("#price", a.el).html("¥" + c).show()
    }, submit: function() {
        var a = this, b = $("#allGame .value", a.el).attr("val"), c = $("#gpit .value", a.el).attr("val"), d = $("#grtit .checked", a.el).attr("val"), e = $("#goods .value", a.el).attr("val"), f = a.options.detailPageURL + "?", g = "gdid=" + b + "/platid=" + c + "/grtp=" + d + "/cateId=" + e;
        f += "?" + util.encode(g), b && c && d && e && window.open(f, "_blank")
    }, eventCode: function() {
        var a = this;
        $(document).on("click", function(a) {
            $(".select-group .data-list").hide()
        }).on("mouseleave", function(a) {
            $(".select-group .data-list").hide()
        }), a.el.on("click", ".select-group .value", function() {
            this.blur()
        }), a.el.on("click", ".select-group:not(.disabled)", function(b) {
            var c = $(this).find(".data-list"), d = c.is(":hidden");
            if ($(".select-group .data-list").hide(), 0 == c.find("li").length)
                return!1;
            switch (d && c.show(), this.id) {
                case"allGame":
                    $("#allGame", a.el).css("z-index", "10"), $("#goods", a.el).css("z-index", "5");
                    break;
                case"goods":
                    $("#allGame", a.el).css("z-index", "5"), $("#goods", a.el).css("z-index", "10")
            }
            a.updateUlPosition($(this), 152), b.stopPropagation()
        }), a.el.on("click", ".select-group .data-list li", function(b) {
            var c = $(this).text(), d = $(this).attr("title"), e = $(this).attr("val");
            d && (c = d), $(this).closest(".select-group").find("input.value").val(c).attr("val", e).css("color", "#666"), $(this).closest("div").is($("#allGame")) && (a.clearSelect(["#gpit .value", "#goods .value"]), a.getGpitByGdid(), $("#gpit .data-list li", a.el).length && $("#gpit .data-list li:first", a.el).click()), $(this).closest("div").is($("#gpit")) && (a.clearSelect(["#goods .value"]), a.getGrtitByPlatid()), $(this).closest("div").is($("#goods")) && (a.clearSelect([""]), a.getPrice()), $(this).closest(".data-list").hide(), b.stopPropagation()
        }), a.el.on("click", ".radio", function() {
            $(this).hasClass("checked") || ($(this).closest("div").find("a").removeClass("checked"), $(this).addClass("checked"), a.clearSelect(["#goods .value"]), a.getFaceValueByGrtit())
        }), a.el.on("click", "#nowBuy", function() {
            a.submit()
        })
    }});